package com.gtfrank.web.servlet.day2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: TestRequestHeadServlet
 * @PackageName:com.gtfrank.web.servlet.day2
 * @Description: 测试请求头的Servlet
 * @Author: frank
 * @Date: 2021/4/28 14:43
 * @Version v1.0
 */
@WebServlet(value = "/day2/TestRequestHeadServlet")
public class TestRequestHeadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("    <meta charset=\"UTF-8\">");
        out.println("    <title>Title</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("    <h2 align=\"center\">打印有关请求头的信息</h2>");
        out.println("    <hr>");
        Enumeration enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()){
            String name = (String)enumeration.nextElement();
            Object value = request.getHeader(name);
            out.println(name+"=======：：：：：：：："+value);
            out.println("<br>");
        }
        out.println("</body>");
        out.println("</html>");


        out.flush();
        out.close();
    }
}
